<?php
	
	//namespace Login\Model;
	//require_once '\Models\DBConnection.php';
	require_once '\Models\User.php';	
		
	class LoginModel{
		
		private $LoginSession = "LoginHandlerLoggedIn";
		
		private $m_connection = NULL;	
		
		/**
		 * Constructor
		 */
		public function __construct(DBConnection $connection)
		{
			$this->m_connection = $connection;
		}
		
		public function Login($user, $pass)
		{
			$sql = "SELECT * FROM user WHERE Username=? AND Password=?";
						
			$stmt = $this->m_connection->Prepare($sql);
			
			$stmt->bind_param("ss", $user, $pass);
						
			$stmt->execute();
			
			$result = $stmt->get_result();
			
			$row_count = $result->num_rows;
			
			if($row_count == 1)
			{
				$_SESSION[$this->LoginSession] = "loggedin";
				$result->close();
				return true;
			}
			$result->close();
			return FALSE;
		}		
		public function Logout()
		{
			if(isset($_SESSION[$this->LoginSession]) == true){
				unset($_SESSION[$this->LoginSession]);
			}
		}
		
		public function IsLoggedIn()
		{
			if(isset($_SESSION[$this->LoginSession]) == true){
				return true;
			}
			return false;
		}
		
	}
